package ru.ifmo.croak.impl.ui.query.team1;

import ru.ifmo.croak.api.QueryRequest;

import javax.swing.*;
import java.awt.event.*;

public class SubscriptionEditorForm extends JDialog {
    private JPanel contentPane;
    private JButton buttonOK;
    private JButton buttonCancel;
    private JTextField authorTextField;
    private JTextArea tagsTextArea;
    private JSpinner quantitySpinner;
    private JButton buttonRemove;


    public SubscriptionEditorForm(JFrame owner) {
        super(owner, "Edit subscription");
        setContentPane(contentPane);
        setModal(true);
        getRootPane().setDefaultButton(buttonOK);

        buttonOK.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onOK();
            }
        });

        buttonCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        });
        buttonRemove.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onRemove();
            }
        });

        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onCancel();
            }
        });

        contentPane.registerKeyboardAction(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        onCancel();
                    }
                }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

        quantitySpinner.setModel(new SpinnerNumberModel(10, 1, 100, 1));
    }


    private void onOK() {
        setVisible(false);
        if (authorTextField.getText().trim().isEmpty()) {
            queryRequest.setAuthor(null);
        } else {
            queryRequest.setAuthor(authorTextField.getText().trim());
        }
        if (tagsTextArea.getText().trim().isEmpty()) {
            queryRequest.setText(null);
        } else {
            queryRequest.setText(tagsTextArea.getText().trim());
        }

        queryRequest.setLimit((Integer) quantitySpinner.getValue());
        callback.editingComplete(queryRequest, false, false);
    }


    private void onCancel() {
        setVisible(false);
        callback.editingComplete(queryRequest, false, true);
    }

    private void onRemove() {
        setVisible(false);
        callback.editingComplete(queryRequest, true, false);
    }


    SubscriptionEditor.Callback callback;
    QueryRequest queryRequest;

    public void prepareAndDisplay(QueryRequest queryRequest, SubscriptionEditor.Callback callback) {
        this.queryRequest = queryRequest;
        this.callback = callback;
        authorTextField.setText(queryRequest.getAuthor());
        tagsTextArea.setText(queryRequest.getText());
        quantitySpinner.setValue(queryRequest.getLimit());
        pack();
        setVisible(true);
    }
}
